//
// Created by win10 on 2024/11/15.
//
#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode{
  int data;
  struct ListNode *next;
} ListNode;

// 创建循环链表
ListNode *createListNode(int n){
  ListNode *head = NULL;
  ListNode *prev = NULL;

  for(int i = 1; i <= n; i++){
    ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
    newNode->data = i;
    newNode->next = NULL;
    if(head == NULL){
    head = newNode;
    }else{
      prev->next = newNode;
    }
    prev = newNode;
  }
  prev->next = head;
  return head;
}

int josephusProblem(ListNode *head, int m){
  ListNode *prev = NULL;
  ListNode *p = head;
  while(p->next != p){
    for(int i = 1; i < m; i++){
      prev = p;
      p = p->next;
    }
    // 删除报数到m的节点
    prev->next = p->next;
    free(p);
    p = prev->next;
  }
  int winner = p->data;
  free(p);
  return winner;
}

int main(){
  int n,m;
  while(1){
    scanf("%d%d",&n,&m);
    if(n == 0 && m == 0){
      break;
    }
    ListNode *circularList = createListNode(n);
    int king = josephusProblem(circularList,m);
    printf("%d\n",king);
  }
  return 0;
}
